package com.zw.yzk.kabu.mapper;

import com.zw.yzk.kabu.data.network.entity.CosplayDetailsEntity;
import com.zw.yzk.kabu.model.CosDetailsCommentModel;
import com.zw.yzk.kabu.utils.TimeFormatUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by zhanwei on 2017/2/25.
 */

public class CosDetailsCommentMapper {

    @Inject
    public CosDetailsCommentMapper() {

    }

    public CosDetailsCommentModel transfer(CosplayDetailsEntity.Comment comment) {
        CosDetailsCommentModel model = new CosDetailsCommentModel();
        model.setComment(comment.comment);
        model.setCommentId(comment.commentId);
//        model.setTime(comment.time);
        try {
            model.setTime(TimeFormatUtils.getTimeInterval(comment.time, "yyyy-MM-dd HH:mm:ss"));
        } catch (Exception e) {
            model.setTime(comment.time);
        }
        model.setCosId(comment.cosId);
        model.setReplyId(comment.replyId);
        model.setUserId(comment.userId);
        model.setName(comment.commentator);
        model.setHead(comment.commentatorHead);
        model.setShowName(comment.commentator);
        model.setReply(false);

        return model;
    }

    public List<CosDetailsCommentModel> transfer(List<CosplayDetailsEntity.Comment> comments) {
        List<CosDetailsCommentModel> models = new ArrayList<>();
        for (CosplayDetailsEntity.Comment comment : comments) {
            models.add(transfer(comment));
        }

        dealCommentList(models);
        return models;
    }

    private void dealCommentList(List<CosDetailsCommentModel> comments) {
        for (CosDetailsCommentModel m1 : comments) {
            for (CosDetailsCommentModel m2 : comments) {
                if (m2.getReplyId() == m1.getCommentId()) {
//                    m2.setShowName(m2.getName() + " 回复 " + m1.getName());
                    m2.setComment("回复" + m1.getName() + ":" + m2.getComment());
                    m2.setReply(true);
                    m2.setReplyId(m1.getUserId());
                }
            }
        }
    }
}
